Inheritance in Python: Understanding the Concept With Examples

Inheritance in Python: Understanding the Concept With Examples

Edited By Team Careers360 | Updated on Feb 05, 2024 04:57 PM IST | #Python

Inheritance is a fundamental concept in object-oriented programming (OOP) that allows a class (subclass or derived class) to inherit properties and behaviours from another class (superclass or base class). In class inheritance Python example is a powerful mechanism that promotes code reuse, modularity, and extensibility.

Inheritance in Python: Understanding the Concept With Examples
Inheritance in Python: Understanding the Concept With Examples

This article explores the concept of inheritance in Python example programs, its types, and important aspects such as the __init__ function, inheritance python example, types of inheritance, and the super() function. But before starting the preparation regarding inheritance with example in Python, consider learning these python certification courses for strengthening your programming and practical skills.

What is Inheritance?

Inheritance is a key feature of OOP that enables the creation of a new class using the properties and methods of an existing class. The existing class is referred to as the superclass, and the new class is known as the subclass. The subclass inherits attributes and behaviours from the superclass, allowing for code reuse and the creation of a hierarchy of classes.

The __init__ Function in Python

The __init__ method is a special method in Python classes that is called when an object is created from a class. It is commonly used for initialising the attributes of an object. In the context of inheritance, the __init__ method of the superclass can be invoked explicitly using the super() function in the __init__ method of the subclass, ensuring that both the subclass and superclass initialisations are performed.

class Superclass:

def __init__(self, parameter):

self.parameter = parameter

class Subclass(Superclass):

def __init__(self, parameter, additional_parameter):

super().__init__(parameter)

self.additional_parameter = additional_parameter

Also read:

Types of Inheritance

Types of inheritance define the relationships between classes and determine how properties are inherited and shared within a codebase. Whether it is single inheritance, where a class inherits from a single superclass, or the more detailed scenarios of multiple, multilevel, hierarchical, and hybrid inheritances, each type brings its own set of advantages and considerations.

Understanding these types of inheritance in Python with example is essential for crafting flexible and maintainable code, enabling developers to create efficient and modular solutions in the world of Python programming.

1. Single Inheritance

In single inheritance, a subclass inherits from only one superclass. It forms a linear hierarchy where a class inherits from another class, creating a single chain of inheritance. The following highlights single inheritance example in Python:

class Animal:

def speak(self):

print("Animal speaks")

class Dog(Animal):

def bark(self):

print("Dog barks")

2. Multiple Inheritance

Multiple inheritance allows a class to inherit from more than one superclass. While it provides flexibility, it can lead to ambiguity if the same method or attribute is present in multiple superclasses. The following program highlights the Multiple inheritance with example in Python:

class A:

def method(self):

print("Method in class A")

class B:

def method(self):

print("Method in class B")

class C(A, B):

pass

3. Multilevel Inheritance

In multilevel inheritance, a class inherits from another class, and then another class inherits from the derived class, forming a chain of inheritance. The following program showcases the multilevel inheritance with example in Python:

class A:

pass

class B(A):

pass

class C(B):

pass

4. Hierarchical Inheritance

In hierarchical inheritance, multiple subclasses inherit from a single superclass. It forms a tree-like structure where one superclass has multiple subclasses. The following program showcases the hierarchical inheritance with example in Python:

class Shape:

def draw(self):

pass

class Circle(Shape):

pass

class Square(Shape):

pass

5. Hybrid Inheritance

Hybrid inheritance is a combination of two or more types of inheritance. It often involves using multiple inheritance along with single or multilevel inheritance. The following program hybrid inheritance in Python with example:

class A:

pass

class B(A):

pass

class C(A):

pass

class D(B, C):

Pass

Also read:

Inheritance Function In Python With Examples

Inheritance is a cornerstone of object-oriented programming (OOP), and Python provides a robust implementation of this concept. Through inheritance, classes can inherit attributes and behaviours from other classes, fostering code reuse and creating a hierarchical structure.

Basics of Inheritance in Python

At its core, inheritance in Python involves creating a new class (subclass) that derives from an existing class (superclass). The subclass inherits attributes and methods from the superclass, allowing for the reuse of code and the establishment of a relationship between the two classes.

Let us start with a simple example of inheritance in python:

class Animal:

def speak(self):

print("Animal speaks")

class Dog(Animal):

def bark(self):

print("Dog barks")

# Creating an instance of the Dog class

dog_instance = Dog()

# Accessing methods from the superclass

dog_instance.speak() # Output: Animal speaks

# Accessing methods from the subclass

dog_instance.bark() # Output: Dog barks

In this explanation inheritance in Python with an example, the Dog class inherits from the Animal class. As a result, an instance of Dog can access both the speak method from the Animal class and the bark method specific to the Dog class.

Also Read:

The super() Function in Python

The super() function is used to call a method or access an attribute from the superclass. It is commonly used in the __init__ method of a subclass to invoke the superclass's __init__ method. The following program represents the super()Function inheritance with example in Python:

class A:

def __init__(self):

print("Initializing class A")

class B(A):

def __init__(self):

super().__init__()

print("Initializing class B")

Here is a code illustration :

class Animal:

def __init__(self, name):

self.name = name

def speak(self):

print(f"{self.name} makes a sound")

class Dog(Animal):

def __init__(self, name, breed):

# Using super() to call the __init__ method

super().__init__(name)

self.breed = breed

def speak(self):

# Using super() to call the speak method

super().speak()

print(f"{self.name} barks")

# Create an instance of the Dog class

my_pet = Dog(name="Buddy", breed="Golden Retriever")

# Call the speak method of the Dog class

my_pet.speak()

Also Read: Free Python Courses & Certifications

Python Method Overriding

Method overriding occurs when a subclass provides a specific implementation for a method that is already defined in its superclass. This allows the subclass to customise or extend the behaviour of the inherited method. This inheritance Python example highlights the Python Method Overriding program:

class A:

def method(self):

print("Method in class A")

class B(A):

def method(self):

print("Method in class B")

# Creating an object of class B

obj = B()

# Calling the overridden method

obj.method()

Related: Popular Programming Courses From Top Providers

Conclusion

Inheritance is a powerful concept in Python that enhances code organisation, reuse, and extensibility. Understanding the types of inheritance, the role of the __init__ method, the use of the super() function, example of multiple inheritance in python and method overriding is crucial for effective object-oriented programming in Python.

By understanding these multilevel inheritance example in Python to multiple inheritance in Python example program, Web developers can create well-structured and modular code that promotes code reusability and maintainability.

Frequently Asked Questions (FAQs)

1. What is inheritance in Python, and why is it important?

It is a fundamental concept in Python's object-oriented programming paradigm, allowing classes to inherit attributes and behaviours from other classes.

2. How does the __init__ function relate to inheritance in Python?

The __init__ function is a special method in Python classes that is used for initialising object attributes. In the context of inheritance, it can be invoked using the super() function to ensure both subclass and superclass initializations are performed.

3. What are the main types of inheritance in Python?

Python supports various types of inheritance, including Single Inheritance, Multiple Inheritance, Multilevel Inheritance, Hierarchical Inheritance, and Hybrid Inheritance. 

4. How does the super() function work in Python?

It is used to call a method or access an attribute from the superclass within a subclass. It is commonly employed in the __init__ method to invoke the superclass's __init__ method explicitly.

5. What are the advantages of using inheritance in Python programming?

Inheritance offers several advantages, including code reusability, extensibility, the creation of well-organised class hierarchies, and improved maintainability.

Articles

Have a question related to Python ?
Udemy 160 courses offered
Eduonix 14 courses offered
Coursera 12 courses offered
Mindmajix Technologies 10 courses offered
Back to top